package com.etc.service.impl;

import com.etc.dao.UsersMapper;
import com.etc.entity.Users;
import com.etc.service.UserService;
import com.etc.util.Md5PasswordUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * zj 用户业务层
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UsersMapper dao;
    @Override
    public boolean deleteByPrimaryKey(Integer uId) {
        int i =0;
        if (uId!=null){
            i=dao.deleteByPrimaryKey(uId);
        }
        return i>0;
    }

    @Override
    public boolean insert(Users record) {
        int i = 0;
        if (record!=null){
            i=dao.insert(record);
        }
        return i>0;
    }

    @Override
    public Users selectByPrimaryKey(Integer uId) {
        Users users=null;
        if (uId!=null){
             users = dao.selectByPrimaryKey(uId);
        }
        return users;
    }

    @Override
    public Users getlogin(String username, String userpwd) {
        Users user = dao.userLogin(username);
        boolean b = Md5PasswordUtil.verify(userpwd, user.getuPassword());
        if(b){
            return user;
        }
        return null;
    }


    /**
     * dhx
     *修改个人信息
     * @param u_id
     * @param u_name
     * @param u_password
     * @param u_sex
     * @return
     */

    @Override
    public boolean getModUsers(String u_id, String u_name, String u_password, String u_sex) {
        int x=dao.queryModUsers(u_id,u_name,u_password,u_sex);

        return x>0;
    }

    @Override
    public PageInfo<Users> getPage(String userName, Integer num, Integer size) {
        PageHelper.startPage(num, size);
        List<Users> list = dao.querylike(userName);
        PageInfo<Users> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean regist(Users user) {
        user.setuPassword(Md5PasswordUtil.generate(user.getuPassword(), Md5PasswordUtil.randomSalt()));
        int n = dao.regist(user);
        return  n>0;
    }
    /**
     * 根据用户编号修改状态
     * 邓红学
     * @param uId 用户编号
     * @return boolean int>0 为True  否则为False
     */
    @Override
    public int modifyStatus(String uId) {

        return dao.modifyStatus(uId);
    }

    @Override
    public boolean upuseruLe(Integer uid, Integer uleve) {
        if (uid!=null && uleve!=null){
            return dao.upuserstuta(uid, uleve)>0;
        }
        return false;
    }


}
